/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2006
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
 
package br.ufmg.dees.insane.draw;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ListIterator;
import java.util.Vector;

import br.ufmg.dees.insane.model.ModelState;

/**
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since	June 2004
 */
public abstract class GridSupportDraw extends Draw {
    
	private static ModelState state;
	
//*****************************************************************************
	
	public void setModelState(ModelState a) {
		state = a;
	}
	
//*****************************************************************************
	
	public ModelState getModelState() {
		return state;
	}
	
//*****************************************************************************
//*****************************************************************************
	
    public static class XYZ extends GridSupportDraw {
        
        public XYZ() {
            this(new Point(0, 0), 0);
        }
		
	//*****************************************************************************
		
        public XYZ(Point loc) {
            this(loc, 0);
        }
        
	//*****************************************************************************
		
        public XYZ(Point loc, double theta) {
            this(loc, theta, null);
        }
        
	//*****************************************************************************
		
        public XYZ(Point loc, double theta, Object represents) {
            this.setLocation(loc);
            this.setAngle(theta);
            this.setRepresents(represents);
            this.setSize(new Dimension(4, 32));
        }
        
	//*****************************************************************************
		
        public Vector<Object> getShapes() {return null;}
		
	//*****************************************************************************
		
		public void draw(Graphics2D g2) {
            
            Vector<Object> shapes = new Vector<Object>();
			Vector<Object> shapes2 = new Vector<Object>();
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
            AffineTransform rot = new AffineTransform();
			rot.rotate(this.getAngle()*(-1), getLocation().x, getLocation().y);
			
			double H = 20 * scale;
			double dist = 5 * scale;
			
			//Drawing the liberation representation
			shapes.clear();
			shapes2.clear();
			
			//Drx + Drz
            shapes.add(new Line2D.Double(x-H/2, y-H/2, x+H/2, y-H/2));
			shapes.add(new Line2D.Double(x+H/2, y-H/2, x+H/2, y+H/2));
			shapes.add(new Line2D.Double(x+H/2, y+H/2, x-H/2, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y+H/2, x-H/2, y-H/2));
			shapes2.add(new Rectangle2D.Double(x-H/2, y-H/2, H, dist));
			shapes2.add(new Rectangle2D.Double(x-H/2, y+H/2-dist, H, dist));
			shapes2.add(new Rectangle2D.Double(x-H/2, y-H/2, dist, H));
			shapes2.add(new Rectangle2D.Double(x+H/2-dist, y-H/2, dist, H));
			
			//Dy
			H = H - 2*dist;
			shapes.add(new Ellipse2D.Double(x-H/2, y-H/2, H, H));
			shapes.add(new Line2D.Double(x, y-H/2, x, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y, x+H/2, y));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2))));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2))));
			
            g2.setColor(state.supportColor);
			ListIterator shps = shapes.listIterator();
			while(shps.hasNext()){
				g2.draw(rot.createTransformedShape((Shape)shps.next()));
			}
			shps = shapes2.listIterator();
			while(shps.hasNext()){
				g2.fill(rot.createTransformedShape((Shape)shps.next()));
			}
        }
        
	//*****************************************************************************
		
        /**
         * Returns the draw bounds in device units.
         *
         * @return the draw bounds
         */
        public Rectangle getBounds() {
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            int width = this.getSize().width;
            int height = this.getSize().height;
            double scale = this.getScale();
			
            int W = (int)(width * scale);
            int H = (int)(height * scale);
            return new Rectangle(x - W - 1, y - H/2 - 1, W + 1, H + 1);
        }
        
    }
    
//*****************************************************************************
//*****************************************************************************
	
    public static class XY extends GridSupportDraw {
         
        public XY() {
            this(new Point(0, 0), 0);
        }
        
	//*****************************************************************************
		
        public XY(Point loc) {
            this(loc, 0);
        }
        
	//*****************************************************************************
		
        public XY(Point loc, double theta) {
            this(loc, theta, null);
        }
        
	//*****************************************************************************
		
        public XY(Point loc, double theta, Object represents) {
            this.setLocation(loc);
            this.setAngle(theta);
            this.setRepresents(represents);
            this.setSize(new Dimension(32,24));
        }
        
	//*****************************************************************************
		
        public Vector<Object> getShapes() {return null;}
		
	//*****************************************************************************
		
		public void draw(Graphics2D g2) {
            
            Vector<Object> shapes = new Vector<Object>();
			Vector<Object> shapes2 = new Vector<Object>();
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
            AffineTransform rot = new AffineTransform();
			rot.rotate(this.getAngle()*(-1), getLocation().x, getLocation().y);
			
			double H = 20 * scale;
			double dist = 5 * scale;
			
			//Drawing the liberation representation
			shapes.clear();
			shapes2.clear();
			
			//Drx
            shapes.add(new Line2D.Double(x-H/2, y-H/2, x+H/2, y-H/2));
			shapes.add(new Line2D.Double(x+H/2, y-H/2, x+H/2, y+H/2));
			shapes.add(new Line2D.Double(x+H/2, y+H/2, x-H/2, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y+H/2, x-H/2, y-H/2));
			shapes2.add(new Rectangle2D.Double(x-H/2, y-H/2, H, dist));
			shapes2.add(new Rectangle2D.Double(x-H/2, y+H/2-dist, H, dist));
			
			//Dy
			H = H - 2*dist;
			shapes.add(new Ellipse2D.Double(x-H/2, y-H/2, H, H));
			shapes.add(new Line2D.Double(x, y-H/2, x, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y, x+H/2, y));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2))));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2))));
			
            g2.setColor(state.supportColor);
			ListIterator shps = shapes.listIterator();
			while(shps.hasNext()){
				g2.draw(rot.createTransformedShape((Shape)shps.next()));
			}
			shps = shapes2.listIterator();
			while(shps.hasNext()){
				g2.fill(rot.createTransformedShape((Shape)shps.next()));
			}
        }
        
	//*****************************************************************************
		
        /**
         * Returns the draw bounds in device units.
         *
         * @return the draw bounds
         */
        public Rectangle getBounds() {
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            int width = this.getSize().width;
            int height = this.getSize().height;
            double scale = this.getScale();
			
            int W = (int)(width * scale);
            int H = (int)(height * scale);
            return new Rectangle((int)(x - W/2), y, W, H);
        }
        
    }
    
//*****************************************************************************
//*****************************************************************************
	
    public static class XZ extends GridSupportDraw {
        
        public XZ() {
            this(new Point(0, 0), 0);
        }
		
	//*****************************************************************************
		
        public XZ(Point loc) {
            this(loc, 0);
        }
        
	//*****************************************************************************
		
        public XZ(Point loc, double theta) {
            this(loc, theta, null);
        }
        
	//*****************************************************************************
		
        public XZ(Point loc, double theta, Object represents) {
            this.setLocation(loc);
            this.setAngle(theta);
            this.setRepresents(represents);
            this.setSize(new Dimension(32, 4));
        }
        
	//*****************************************************************************
		
        public Vector<Object> getShapes() {return null;}
		
	//*****************************************************************************
		
		public void draw(Graphics2D g2) {
            
            Vector<Object> shapes = new Vector<Object>();
			Vector<Object> shapes2 = new Vector<Object>();
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
            AffineTransform rot = new AffineTransform();
			rot.rotate(this.getAngle()*(-1), getLocation().x, getLocation().y);
			
			double H = 20 * scale;
			double dist = 5 * scale;
			
			//Drawing the liberation representation
			shapes.clear();
			shapes2.clear();
			
            shapes.add(new Line2D.Double(x-H/2, y-H/2, x+H/2, y-H/2));
			shapes.add(new Line2D.Double(x+H/2, y-H/2, x+H/2, y+H/2));
			shapes.add(new Line2D.Double(x+H/2, y+H/2, x-H/2, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y+H/2, x-H/2, y-H/2));
			
			shapes2.add(new Rectangle2D.Double(x-H/2, y-H/2, H, dist));
			shapes2.add(new Rectangle2D.Double(x-H/2, y+H/2-dist, H, dist));
			
			shapes2.add(new Rectangle2D.Double(x-H/2, y-H/2, dist, H));
			shapes2.add(new Rectangle2D.Double(x+H/2-dist, y-H/2, dist, H));
			
            g2.setColor(state.supportColor);
			ListIterator shps = shapes.listIterator();
			while(shps.hasNext()){
				g2.draw(rot.createTransformedShape((Shape)shps.next()));
			}
			shps = shapes2.listIterator();
			while(shps.hasNext()){
				g2.fill(rot.createTransformedShape((Shape)shps.next()));
			}
        }
		
	//*****************************************************************************
		
        /**
         * Returns the draw bounds in device units.
         *
         * @return the draw bounds
         */
        public Rectangle getBounds() {
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            int width = this.getSize().width;
            int height = this.getSize().height;
            double scale = this.getScale();
			
            int W = (int)(width * scale);
            int H = (int)(height * scale);
            return new Rectangle((int)(x - W/2 - 1), y -1, W + 1, H*2 +1);
        }
        
    }
    
//*****************************************************************************
//*****************************************************************************
	
    public static class Y extends GridSupportDraw {
         
        public Y() {
            this(new Point(0, 0), 0);
        }
        
	//*****************************************************************************
		
        public Y(Point loc) {
            this(loc, 0);
        }
        
	//*****************************************************************************
		
        public Y(Point loc, double theta) {
            this(loc, theta, null);
        }
        
	//*****************************************************************************
		
        public Y(Point loc, double theta, Object represents) {
            this.setLocation(loc);
            this.setAngle(theta);
            this.setRepresents(represents);
            this.setSize(new Dimension(32, 24));
        }
        
	//*****************************************************************************
		
        public Vector<Object> getShapes() {return null;}
		
	//*****************************************************************************
		
		public void draw(Graphics2D g2) {
            
            Vector<Object> shapes = new Vector<Object>();
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
            AffineTransform rot = new AffineTransform();
			rot.rotate(this.getAngle()*(-1), getLocation().x, getLocation().y);
			
			double H = 20 * scale;
			
			//Drawing the liberation representation
			shapes.clear();
			
            shapes.add(new Ellipse2D.Double(x-H/2, y-H/2, H, H));
			shapes.add(new Line2D.Double(x, y-H/2, x, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y, x+H/2, y));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2))));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2))));
			
            g2.setColor(state.supportColor);
			ListIterator shps = shapes.listIterator();
			while(shps.hasNext()){
				g2.draw(rot.createTransformedShape((Shape)shps.next()));
			}
        }
        
	//*****************************************************************************
		
        /**
         * Returns the draw bounds in device units.
         *
         * @return the draw bounds
         */
        public Rectangle getBounds() {
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            int width = this.getSize().width;
            int height = this.getSize().height;
            double scale = this.getScale();
			
            int W = (int)(width * scale);
            int H = (int)(width * scale);
            int h = (int)(height/6 * scale);
            return new Rectangle((int)(x - W/2), y, W, H - h);
        }
        
    }

//*****************************************************************************
//*****************************************************************************
	
	public static class X extends GridSupportDraw {
        
        public X() {
            this(new Point(0, 0), 0);
        }
		
	//*****************************************************************************
		
        public X(Point loc) {
            this(loc, 0);
        }
        
	//*****************************************************************************
		
        public X(Point loc, double theta) {
            this(loc, theta, null);
        }
        
	//*****************************************************************************
		
        public X(Point loc, double theta, Object represents) {
            this.setLocation(loc);
            this.setAngle(theta);
            this.setRepresents(represents);
            this.setSize(new Dimension(4, 32));
        }
        
	//*****************************************************************************
		
        public Vector<Object> getShapes() {return null;}
		
	//*****************************************************************************
		
		public void draw(Graphics2D g2) {
            
            Vector<Object> shapes = new Vector<Object>();
			Vector<Object> shapes2 = new Vector<Object>();
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
            AffineTransform rot = new AffineTransform();
			rot.rotate(this.getAngle()*(-1), getLocation().x, getLocation().y);
			
			double H = 20 * scale;
			double dist = 5 * scale;
			
			//Drawing the liberation representation
			shapes.clear();
			shapes2.clear();
			
            shapes.add(new Line2D.Double(x-H/2, y-H/2, x+H/2, y-H/2));
			shapes.add(new Line2D.Double(x+H/2, y-H/2, x+H/2, y+H/2));
			shapes.add(new Line2D.Double(x+H/2, y+H/2, x-H/2, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y+H/2, x-H/2, y-H/2));
			
			shapes2.add(new Rectangle2D.Double(x-H/2, y-H/2, H, dist));
			shapes2.add(new Rectangle2D.Double(x-H/2, y+H/2-dist, H, dist));
			
            g2.setColor(state.supportColor);
			ListIterator shps = shapes.listIterator();
			while(shps.hasNext()){
				g2.draw(rot.createTransformedShape((Shape)shps.next()));
			}
			shps = shapes2.listIterator();
			while(shps.hasNext()){
				g2.fill(rot.createTransformedShape((Shape)shps.next()));
			}
        }
        
	//*****************************************************************************
		
        /**
         * Returns the draw bounds in device units.
         *
         * @return the draw bounds
         */
        public Rectangle getBounds() {
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            int width = this.getSize().width;
            int height = this.getSize().height;
            double scale = this.getScale();
			
            int W = (int)(width * scale);
            int H = (int)(height * scale);
            return new Rectangle(x - W - 1, y - H/2 - 1, W + 1, H + 1);
        }
        
    }
	
//*****************************************************************************
//*****************************************************************************
	
	//This class exists just to avoid combinig supports ans spring draws
	//It must be identical to Y class, producing a draw whose size is exactly
	//half of the size of Y
	public static class halfY extends GridSupportDraw {
         
        public halfY() {
            this(new Point(0, 0), 0);
        }
        
	//*****************************************************************************
		
        public halfY(Point loc) {
            this(loc, 0);
        }
        
	//*****************************************************************************
		
        public halfY(Point loc, double theta) {
            this(loc, theta, null);
        }
        
	//*****************************************************************************
		
        public halfY(Point loc, double theta, Object represents) {
            this.setLocation(loc);
            this.setAngle(theta);
            this.setRepresents(represents);
            this.setSize(new Dimension(32, 24));
        }
        
	//*****************************************************************************
		
        public Vector<Object> getShapes() {return null;}
		
	//*****************************************************************************
		
		public void draw(Graphics2D g2) {
            
            Vector<Object> shapes = new Vector<Object>();
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            double scale = this.getScale();
            
            AffineTransform rot = new AffineTransform();
			rot.rotate(this.getAngle()*(-1), getLocation().x, getLocation().y);
			
			double H = 10 * scale;
			
			//Drawing the liberation representation
			shapes.clear();
			
            shapes.add(new Ellipse2D.Double(x-H/2, y-H/2, H, H));
			shapes.add(new Line2D.Double(x, y-H/2, x, y+H/2));
			shapes.add(new Line2D.Double(x-H/2, y, x+H/2, y));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2))));
			shapes.add(new Line2D.Double(x-H/(2*Math.sqrt(2)), y+H/(2*Math.sqrt(2)), x+H/(2*Math.sqrt(2)), y-H/(2*Math.sqrt(2))));
			
            g2.setColor(state.supportColor);
			ListIterator shps = shapes.listIterator();
			while(shps.hasNext()){
				g2.draw(rot.createTransformedShape((Shape)shps.next()));
			}
        }
        
	//*****************************************************************************
		
        /**
         * Returns the draw bounds in device units.
         *
         * @return the draw bounds
         */
        public Rectangle getBounds() {
            
            int x = this.getLocation().x;
            int y = this.getLocation().y;
            int width = this.getSize().width;
            int height = this.getSize().height;
            double scale = this.getScale();
			
            int W = (int)(width * scale);
            int H = (int)(width * scale);
            int h = (int)(height/6 * scale);
            return new Rectangle((int)(x - W/2), y, W, H - h);
        }
        
    }
	
//*****************************************************************************
//*****************************************************************************
}
